package studio.banner.reference.service.impl;


import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import studio.banner.reference.entity.Aggregate;
import studio.banner.reference.entity.Document;
import studio.banner.reference.mapper.AggregateMapper;
import studio.banner.reference.mapper.DocumentMapper;
import studio.banner.reference.service.DocumentService;
import studio.banner.reference.util.WordUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class DocumentServiceImpl extends ServiceImpl<DocumentMapper, Document> implements DocumentService {
    @Autowired
    private DocumentMapper documentMapper;
    @Autowired
    private AggregateMapper aggregateMapper;


    @Override
    public boolean createDocument(Document document) {
        Aggregate aggregate = aggregateMapper.selectById(document.getAggregateId());
        aggregateMapper.updateSize(document.getAggregateId(), 1);
        document.setNumber((byte) (aggregate.getSize() + 1));
        document.setId(0L);
        return documentMapper.insert(document) == 1;
    }

    @Override
    public boolean createDocumentList(List<String> docList, Integer aggregateId) {
        Aggregate aggregate = aggregateMapper.selectById(aggregateId);
        aggregateMapper.updateSize(aggregateId, docList.size());
        Document document;
        int thesisSize = aggregate.getSize();
        for (String docContent : docList) {
            document = new Document(0L, docContent, aggregate.getUserId(), aggregateId, null, (byte) (++thesisSize));
            documentMapper.insert(document);
        }
        return true;
    }

    @Override
    public boolean deleteDocument(Long documentId) {
        Document document = documentMapper.selectById(documentId);
        aggregateMapper.updateSize(document.getAggregateId(), -1);
        new LambdaUpdateChainWrapper<Document>(documentMapper)
                .gt(Document::getNumber, document.getNumber())
                .setSql("number = number - 1").update();
        return documentMapper.deleteById(documentId) == 1;
    }

    @Override
    public boolean removeByAggregateId(Integer id) {
        return new LambdaUpdateChainWrapper<Document>(documentMapper)
                .eq(Document::getAggregateId, id).remove();
    }

    @Override
    public boolean deleteDocumentList(List<Long> documentIds, Integer userId) {
        for (long documentId : documentIds) {
            if (!documentMapper.selectById(documentId).getUserId().equals(userId)) {
                return false;
            }
        }
        for (long documentId : documentIds) {
            deleteDocument(documentId);
        }
        return true;
    }

    @Override
    public boolean updateContent(String documentContent, Long documentId) {
        return new LambdaUpdateChainWrapper<Document>(documentMapper)
                .eq(Document::getId, documentId)
                .set(Document::getDocContent, documentContent).update();
    }

    @Override
    public List<Document> getDocumentByKeyword(String keyWord, Integer userId) {
        return new LambdaQueryChainWrapper<Document>(documentMapper)
                .eq(Document::getUserId, userId)
                .like(Document::getDocContent, keyWord).list();
    }

    @Override
    public List<Document> listByAggregateId(Integer aggregateId) {
        return new LambdaQueryChainWrapper<Document>(documentMapper)
                .eq(Document::getAggregateId, aggregateId).list();
    }

    @Override
    public boolean documentCheck(String documentContent) {
        return false;
    }

    @Override
    public List<Document> wordImport(MultipartFile file) throws IOException {
        WordUtil wordUtil = new WordUtil();
        return wordUtil.toDocContent(wordUtil.wordImport(file));
    }

    @Override
    public void downloadDocuments(List<Integer> documentIds, HttpServletResponse response) throws IOException {

    }


}
